import {
  getMemberInfo,
  pay,
  getMemberOrderId,
  getMemberDeadline
} from "../../../api/user";
import {
  formatPrice,
  getSubscribeMessageAuth
} from "../../../utils/util";

// pages/user/member/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0, // 会员套餐 id
    vip: getApp().globalData.userInfo.vip,
    vipPrice: 0,
    vipPriceMonth: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadMemberInfo();
  },

  /**
   * 确认开通/续费
   */
  async handleDredge() {
    // 申请开通会员通知
    await getSubscribeMessageAuth(['prOBdMPuMmqBnJCz-5ib8dFGIkQ4ybIyVbzzD13eEng'])

    let res = await getMemberOrderId(this.data.id);

    await pay(res.orderId);

    res = await getMemberDeadline();

    getApp().globalData.userInfo.vip = true;
    getApp().globalData.userInfo.vipDeadline = res.deadline;
    
    wx.showToast({
      title: '支付成功',
      icon: 'success',
      mask: true,
    })
    let timer = setTimeout(() => {
      clearTimeout(timer);
      wx.navigateBack({});
    }, 2000);

  },

  /**
   * 显示会员价格
   */
  loadMemberInfo() {
    getMemberInfo().then(res => {
      this.data.id = res.id;
      this.setData({
        vipPrice: formatPrice(res.price),
        vipPriceMonth: formatPrice(res.price / 12),
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})